﻿
$(function () {
    $(".regular_checkall").change(function () {
        var checked = $(this).prop("checked");
        var targetContainer = $(this).attr("data-target-container")
        if (!targetContainer)
            targetContainer = $("body");
        $("input[type='checkbox']", targetContainer).prop("checked", checked);
    });
    $(".listview_checkall").change(function () {
        var checked = $(this).prop("checked");
        $(this).closest(".mainlistview").find(".selectrow_checkbox >input[type='checkbox']").prop("checked", checked);
    });
    $(".listview_modifybutton").click(function () {
        var length = 0;
        $(".mainlistview .selectrow_checkbox >input[type='checkbox']").each(function () {
            if ($(this).prop("checked")) {
                length++;
            }
        });
        if (length <= 0) {
            alert("请至少选择一项！");
            return false;
        } else if (length > 1) {
            alert("只允许选择一项！");
            return false;
        }
    });
    $(".listview_deletebutton").click(function () {
        var length = 0;
        $(".mainlistview .selectrow_checkbox >input[type='checkbox']").each(function () {
            if ($(this).prop("checked")) {
                length++;
            }
        });
        if (length <= 0) {
            alert("请选择需要删除的项！");
            return false;
        }
        return window.confirm("你确认删除吗？");
    });
    $(".listview_gopagebutton").click(function () {
        if ($(".GoPage").val() == "") {
            alert("请输入跳转的页码！");
            $(".GoPage").focus();
            return false;
        }
        if ($(".TxtPageSize").val() == "") {
            alert("请输入每页行数！");
            $(".TxtPageSize").focus();
            return false;
        }
        return true;
    });

    $(".gobackbutton").click(function () {
        window.history.go(-1);
        return false;
    });
    $(".printbutton").click(function () {
        window.print();
        return false;
    });
    $(".shezhiquanb").click(function () {


        $(".lookuptarget").val("全部");

    })

    // Look up Dialog
    // Please use LookupHyperLink
    $(".lookupbutton[data-lookupotions]").click(function () {
        var lookupOptions = $.extend({
            DialogWidth: "350px",
            DialogHeight: "400px",
            DialogResizable: false,
            OriginalValue: ""
            //MergeValue: false
        }, $.parseJSON($(this).attr("data-lookupotions")));
        var random = new Date().getMilliseconds();
        var url = lookupOptions.DialogURL + (lookupOptions.DialogURL.indexOf('?') >= 0 ? '&' : '?') + '__t=' + random;

        var currentValue = $(lookupOptions.LookupTarget).val() ||  lookupOptions.OriginalValue;

        if (currentValue) {
            url += "&value=" + encodeURIComponent(currentValue);
        }
        var wName = window.showModalDialog(url,
                                            top,
                                            'dialogWidth:' + lookupOptions.DialogWidth + ';DialogHeight=' + lookupOptions.DialogHeight + ';status:no;help:no;resizable:' + lookupOptions.DialogResizable + ';');
        if (wName != null)
            $(lookupOptions.LookupTarget).val(wName);
    });

    /* Look up Dialog Button Logic Starts */
    var getChildNodesCheckBoxesOfTreeView = function ($checkbox) {
        var $id = $($checkbox).attr("id");
        var subNodesDivID = $id.replace("CheckBox", "Nodes");
        return $("#" + subNodesDivID).find("input[type='checkbox']");
    }
    $(".selectform .selectsource input[type='checkbox']").change(function () {
        var $id = $(this).attr("id");
        var checked = $(this).prop("checked");
        // is tree view
        if (!$(this).closest(".selectsource").hasClass("gridview")) {
            getChildNodesCheckBoxesOfTreeView($(this)).prop("checked", checked);
        }
    });

    var getSelectedValuesOfSelectForm = function ($selectSource) {
        var result = "";
        // is gridview
        if ($($selectSource).hasClass("gridview")) {
            $($selectSource).find("input[type='checkbox']:not(.listview_checkall)").each(function () {
                if ($(this).prop("checked")) {
                    if (result.length > 0)
                        result += ",";
                    result += $(this).closest("td").find(".selectvalue").val();
                }
            });
        }
        // is treeview
        else {
            $($selectSource).find("input[type='checkbox']").each(function () {
                if (getChildNodesCheckBoxesOfTreeView($(this)).length <= 0 && $(this).prop("checked")) {
                    if (result.length > 0)
                        result += ",";
                    result += $(this).attr("title");
                }
            });
        }
        return result;
    }
    $(".selectform .submitbutton").click(function () {
        window.returnValue = getSelectedValuesOfSelectForm(".selectsource");
        window.close();
    });
    $(".selectform .cancelbutton").click(function () {
        window.returnValue = "";
        window.close();
    });
    /* Look up Dialog Button Logic Ends */


    /*DateTime Picker*/
    $(".datetimepicker,.datepicker,timepicker").each(function(i,n){
        var nextSlibing = $(n).next();
        if (!(nextSlibing.is("a") && nextSlibing.hasClass("calendar_icon"))) {
            var calendarIcon = $('<a class="calendar_icon"><span class="icon_button"></span></a>');
            $(n).after(calendarIcon);
            if (!$(n).prop("disabled")) {
                $(calendarIcon).click(function () {
                    $(n).focus();
                });
            }
        }
    }); 
    $(".datetimepicker").datetimepicker();
    $(".datepicker").datepicker();
    $(".timepicker").timepicker();
})